package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	//第一题请问 s1、s2内各有什么元素？
	// s1修改一个元素会影响s2吗？s2修改一个元素会影响s1吗？
	// s2再增加一个元素会怎么样？
	s1 := make([]int, 3, 4)
	s2 := append(s1, 1)
	fmt.Printf("s1: %v\n", s1) //答：s1 中有3个0值，
	fmt.Printf("%+v\n", s2)    // s2 中有 3个0值 1 个1
	s1[0] = 1
	s2[2] = 2
	fmt.Printf("s1: %v\n", s1)
	fmt.Printf("%+v %p %p\n", s2, &s2, &s2[0]) //s1 修改会影响s2,s2 修改一个元素会影响s1
	//
	s2 = append(s2, 5)
	fmt.Printf("%+v %p %p \n", s2, &s2, &s2[0]) //s2 增加一个元素会扩容
	//有一个数组 [1,4,9,16,2,5,10,15]，生成一个新切片，要求新切片元素是数组相邻2项的和。
	array := [8]int{1, 4, 9, 16, 2, 5, 10, 15}
	ns := make([]int, 0, 8)
	ar := array[:]
	newSplice(&ar, &ns)
	fmt.Printf("%+v\n", ns)
	//数字重复统计
	countingDuplicateDataInRandomNumbers(100)
}

func countingDuplicateDataInRandomNumbers(c int) {
	rs := make([]int, 0, c)
	for i := 0; i < 99; i++ {
		rs = append(rs, randdom(100, 201))
	}
	m := make(map[int]int)
	for _, element := range rs {
		_, ok := m[element]
		if ok {
			m[element]++
		} else {
			m[element] = 1
		}

	}
	for k, v := range m {
		fmt.Printf("key:%+v,count:%d\n", k, v)
	}
}
func randdom(start int, end int) int {
	n := rand.NewSource(time.Now().UnixNano())
	rand.New(n)
	return rand.Intn(end) - start
}
func newSplice(s *[]int, r *[]int) {
	array := *s
	for i := 0; i < len(array)-1; i++ {
		v := array[i]
		n := array[i+1]
		*r = append(*r, v+n)
	}
}
